package com.bitcat.base

import android.app.Application
import com.bitcat.base.widget.EmptyCallback
import com.bitcat.base.widget.ErrorCallback
import com.bitcat.base.widget.LoadingCallback
import com.elvishew.xlog.LogConfiguration
import com.elvishew.xlog.LogLevel
import com.elvishew.xlog.XLog
import com.kingja.loadsir.callback.SuccessCallback
import com.kingja.loadsir.core.LoadSir


val app by lazy { BaseApp.instance }

open class BaseApp : Application() {

    override fun onCreate() {
        super.onCreate()
        instance = this
        initLog()
        initLoadSir()
    }

    private fun initLoadSir() {
        LoadSir.beginBuilder()
            .addCallback(LoadingCallback()) //加载中
            .addCallback(EmptyCallback()) // 空数据
            .addCallback(ErrorCallback()) // 错误
            .setDefaultCallback(SuccessCallback::class.java) //默认显示
            .commit()
    }

    // 初始化日志
    private fun initLog() {
        val logConfig = LogConfiguration.Builder()
            .logLevel(if (BuildConfig.DEBUG) LogLevel.ALL else LogLevel.NONE)
            .tag("WanAndroid LOG")
            .build()

        XLog.init(logConfig)
    }

    //类似Java中的static
    companion object {
        lateinit var instance: BaseApp
    }
}